/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.felinelayer.client.clientSwing;

import br.com.felinelayer.client.IComponentClient;
import br.com.felinelayer.common.Message;
import br.com.felinelayer.common.Protocol;
import javax.swing.JLabel;

/**
 *
 */
public class LabelClient extends ComponentSwing<LabelClient, JLabel> implements br.com.felinelayer.client.ILabelClient {

    @Override
    protected void doInitialize() {
        jLabel = initializeComponente( new JLabel() );
    }
    private JLabel jLabel;

    @Override
    public LabelClient setText( String text ) {
        jLabel.setText( text );
        jLabel.setSize( jLabel.getPreferredSize() );        
        return this;
    }

    @Override
    public String getText() {
        return jLabel.getText();
    }

    @Override
    protected void doChangeProperty( Message message ) {

        if ( message.isPropertyExists( Protocol.PROPERTY_ANCHOR_LEFT ) ) {
            IComponentClient cm = getClient().getComponentById( message.getIntByName( Protocol.PROPERTY_ANCHOR_LEFT ) );
            cm.setLabelAnchorLeft( this );
            updateAnchorLeft( cm );
        }
        if ( message.isPropertyExists( Protocol.PROPERTY_ANCHOR_TOP ) ) {
            IComponentClient cm = getClient().getComponentById( message.getIntByName( Protocol.PROPERTY_ANCHOR_TOP ) );
            cm.setLabelAnchorTop( this );
            updateAnchorTop( cm );
        }
    }

    public void updateAnchorLeft( IComponentClient cm ) {
//        System.out.println( "---------------------------" );
//        System.out.println( "cm: " + cm.getDimStr() );
//        System.out.println( "lb: " + getDimStr() );
        cm.setLabelAnchorLeft( this );
        setLeft( cm.getLeft() - getWidth() - 8 );
        setTop( cm.getTop() );
//        System.out.println( "lb: " + getDimStr() );
    }

    public void updateAnchorTop( IComponentClient cm ) {
        cm.setLabelAnchorTop( this );
        setLeft( cm.getLeft() );
        setTop( cm.getTop() - getHeight() );
    }
}